
package com.viweb.storemanager.common;

import com.l2fprod.common.swing.StatusBar;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;
import javax.swing.JLabel;


public class AppStatusBar {

    private StatusBarFactory statusBarFactory;
    private StatusBar bar;

    /** Creates a new instance of AppStatusBar */
    public AppStatusBar() {
        this.initAppStatusBar();
    }

    private void initAppStatusBar() {
        String TIME_FORMAT = "dd'/'MM'/'yyyy' 'h':'mm' 'a";
        final SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT);
        Calendar calCurrent = Calendar.getInstance(TimeZone.getDefault());
        sdf.setTimeZone(TimeZone.getDefault());

        statusBarFactory = new StatusBarFactory("/com/viweb/storemanager/imgae/");

        final JLabel statusZone = statusBarFactory.addZone("statusZone", "50%", "Sẵn sàng...");
        statusBarFactory.addSeparator("sepIZone", "0.2%", "separator.png");
        final JLabel userZone = statusBarFactory.addZone("userZone", "25%", "NSD: ");
        statusBarFactory.addSeparator("sepIIZone", "0.2%", "separator.png");
        final JLabel timerZone = statusBarFactory.addZone("timerZone", "*", "Thời gian: " + sdf.format(calCurrent.getTime()));

        Runnable r = new Runnable() {

            public void run() {
                try {
                    while (true) {
                        Thread.sleep(1000);
                        Calendar cal = Calendar.getInstance(TimeZone.getDefault());
                        timerZone.setText("Thời gian: " + sdf.format(cal.getTime()));
                    }
                } catch (Exception x) {
                    x.printStackTrace();
                }
            }
        };
        Thread timerThread = new Thread(r);
        timerThread.start();

        bar = statusBarFactory.getStatusBar();
    }

    public void setCurrentStatus(String status) {
        ((JLabel) this.bar.getZone("statusZone")).setText(status);
    }

    public void setCurrentUsername(String username) {
        ((JLabel) this.bar.getZone("userZone")).setText("NSD: " + username);
    }

    public StatusBar getBar() {
        return this.bar;
    }
}
